﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPassword
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("[START]");

            Run();

            Console.WriteLine("[END]");
        }

        static void Run()
        {
            string password = string.Empty;
            bool isValidPassword = false;

            Console.WriteLine("Enter password:");
            password = Console.ReadLine();

            isValidPassword = IsPasswordValid(password);

            if (isValidPassword)
                Console.WriteLine("Password is valid.");
            else
                Console.WriteLine("Password is not valid.");
            
        }

        /// <summary>
        /// Rules
        /// 1.  Minimum password length:  8 characters
        /// 2.  Contains a alpha characters
        /// 3.  Contains a digit character
        /// 4.  Password max age: 90 days
        /// 5.  Password expiry alert: 14 days
        /// 6.  Password min age: 1 day
        /// 7.  Password history: last 8 password
        /// 8.  Password storage: not reversible
        /// 9.  Account lock duration: 30 minutes
        /// 10. Account lockout attempts: 6
        /// 11. Account lockout action: account disabled
        /// 12. Account lockout reset: 30 minutes
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        static bool IsPasswordValid(string pwd)
        {
            bool isPasswordValid = true;
            // bool to track rules
            bool containsAlphaChar = false;
            bool containsDigitChar = false;

            char[] pwdCharArray = pwd.ToCharArray();

            // 1.  Minimum password length:  8 characters
            if (pwd.Length < 8)
            {
                isPasswordValid = false;
                return isPasswordValid;
            }

            // 2.  Contains a alpha characters
            foreach (char c in pwdCharArray)
            {
                if (char.IsDigit(c))
                {
                    containsDigitChar = true;
                    break;
                }
            }
            if (!containsDigitChar)
            {
                isPasswordValid = false;
                return isPasswordValid;
            }

            // 3.  Contains a digit character
            foreach (char c in pwdCharArray)
            {
                if (char.IsLetter(c))
                {
                    containsAlphaChar = true;
                    break;
                }
            }
            if (!containsAlphaChar)
            {
                isPasswordValid = false;
                return isPasswordValid;
            }

            return isPasswordValid;
        }
    }
}
